/* -------------------------------------- 

     Cherrypy webapp skeleton frontend

     License: LGPL
     Authors: saaj (mail@saaj.me)

   -------------------------------------- */

/**
 * Remote table model class 
 */
qx.Class.define("webapp.model.RemoteTable", {

  extend : qx.ui.table.model.Remote,

  properties : {

    serviceName : {deferredInit : true}

  },

  members : {

    _loadRowCount : function()
    {
      var rpc = new webapp.network.Rpc(this.getServiceName());
      rpc.addListener("completed", function(event)
      {
        this._onRowCountLoaded(Number(event.getData().result));
      }, this);
      rpc.addListener("failed", function()
      {
      	this._onRowCountLoaded(0);
      }, this);

      rpc.call("count");
    },

    _loadRowData : function(firstRow, lastRow)
    {
      var rpc = new webapp.network.Rpc(this.getServiceName());
      rpc.addListener("completed", function(event)
      {
        this._onRowDataLoaded(event.getData().result);
      }, this);
      rpc.addListener("failed", function()
      {
        this._onRowDataLoaded(null);
      }, this);

      rpc.call("range", firstRow, lastRow);
    },

    // override
    removeRow : function(rowIndex)
    {
      var rowData = this.getRowData(rowIndex);
      if(rowIndex == -1 || !rowData)
      {
        return;
      }

      this.base(arguments, rowIndex);

      var rpc = new webapp.network.Rpc(this.getServiceName());
      rpc.call("remove", rowData[this.getColumnId(0)]);
    }

  }

});